#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Sat May  5 11:13:39 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/cpufreq/interactive/$2
}

action=$1
setenforce 0

if [ ! -f /dev/project_wipe_runonce ]; then
	# make sure that sysfs is RW
	mount -o remount,rw sysfs /sys
	# in case of using ondemand as default governor
	if [ ! `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor` = "interactive" ]; then 
		set_value "interactive" /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
	fi
fi

chown 0.0 /sys/devices/system/cpu/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpufreq/interactive/*

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	set_value 3 /dev/cpuset/background/cpus
	set_value 2-3 /dev/cpuset/system-background/cpus
	set_value 0-2 /dev/cpuset/foreground/cpus
	set_value 0-2 /dev/cpuset/top-app/cpus

	set_value 280000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu0 io_is_busy 0
fi

if [ "$action" = "powersave" ]; then
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "18000 1180000:38000 1480000:58000 1780000:138000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 97
	set_param cpu0 boostpulse_duration 18000
	set_param cpu0 target_loads "80 380000:28 580000:48 680000:24 880000:67 980000:50 1180000:78 1480000:95"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "38000 1180000:18000 1680000:58000 1780000:138000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 98
	set_param cpu0 boostpulse_duration 58000
	set_param cpu0 target_loads "80 380000:20 580000:39 680000:46 880000:36 980000:45 1180000:85 1480000:90 1680000:98"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "18000 1680000:58000 1780000:38000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 80
	set_param cpu0 boostpulse_duration 78000
	set_param cpu0 target_loads "80 380000:3 680000:48 880000:39 980000:49 1180000:75 1480000:81 1680000:98"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "fast" ]; then
	set_param cpu0 boost 1
	set_param cpu0 above_hispeed_delay "18000"
	set_param cpu0 hispeed_freq 1680000
	set_param cpu0 target_loads "90 1980000:95"
fi

chmod 0444 /sys/devices/system/cpu/cpufreq/interactive/*

exit 0